<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增组织')" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-org-add">
			<input id="treeId" name="parentId" type="hidden" th:value="${org}?${org.orgId}:0"   />
			<div class="form-group">
				<label class="col-sm-3 control-label">组织标识：</label>
				<div th:if="${org != null}" class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getType('sys_org_flag')}">
						<input type="radio" th:id="${'orgFlag_' + dict.dictCode}" name="orgFlag" th:value="${dict.dictValue}" th:field="${org.orgFlag}">
						<label th:for="${'orgFlag_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
				<div th:unless="${org != null}" class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getType('sys_org_flag')}">
						<input type="radio" th:id="${'orgFlag_' + dict.dictCode}" name="orgFlag" th:value="${dict.dictValue}" th:checked="${dict.default}">
						<label th:for="${'orgFlag_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">上级组织：</label>
				<div class="col-sm-8">
				    <div class="input-group">
						<input class="form-control" type="text" onclick="selectOrgTree()" id="treeName" readonly="true" th:value="${org?.orgName}" placeholder="请选择" >
					    <span class="input-group-addon"><i class="fa fa-search"></i></span>
				    </div>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label is-required">组织编号：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="orgNo" id="orgNo" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">组织名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="orgName" id="orgName" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">显示排序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="orderNum" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">负责人：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="leader">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">联系电话：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="phone">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">邮箱：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="email">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">组织状态：</label>
				<div class="col-sm-8">
				    <div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
						<input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.default}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
		var prefix = ctx + "system/org";
		// $("input[name='orgFlag']").iCheck('disable');  // 将选框禁用
		// ifChanged 触发时机：选框状态改变时触发
		// $("input[name='orgFlag']").on('ifChecked', function(obj){
		// 	// console.log($(this).val());              // 获取该复选框的value值
		// 	// console.log(obj.currentTarget.checked)   // 获取该复选框当前状态是否选中
		// 	// console.log('您更改了复选框的状态');
		// 	var orgFlag = $(this).val();
		// 	var parentId = $("input[name='parentId']").val();
		// 	console.log(orgFlag,parentId);
		// 	if(orgFlag == '1' && parentId == 0){
		// 		$.modal.alertWarning("请选择父级单位！");
		// 	}
		// })
		$("#form-org-add").validate({
			onkeyup: false,
			rules:{
				// orgName:{
				// 	remote: {
		        //         url: prefix + "/checkOrgNameUnique",
		        //         type: "post",
		        //         dataType: "json",
		        //         data: {
		        //         	"parentId": function() {
		        //         		return $("input[name='parentId']").val();
		        //             },
		        //         	"orgName" : function() {
		        //                 return $.common.trim($("#orgName").val());
		        //             }
		        //         }
		        //     }
				// },
				orgNo:{
					remote: {
		                url: prefix + "/checkOrgNoUnique",
		                type: "post",
		                dataType: "json",
		                data: {
		                	"parentId": function() {
		                		return $("input[name='parentId']").val();
		                    },
		                	"orgNo" : function() {
		                        return $.common.trim($("#orgNo").val());
		                    }
		                }
		            }
				},
				orderNum:{
					digits:true
				},
				email:{
                    email:true,
        		},
        		phone:{
        			isPhone:true,
        		},
			},
			messages: {
		        "orgNo": {
		            remote: "组织编号已经存在"
		        },
		        // "orgName": {
		        //     remote: "组织已经存在"
		        // }
		    },
		    focusCleanup: true
		});
		
		function submitHandler() {
			var orgFlag = $("input[name='orgFlag']:checked").val();
			var parentId = $("input[name='parentId']").val();
			if(orgFlag == '1' && parentId == 0){
				$.modal.alertWarning("请选择父级单位！");
				return;
			}
	        if ($.validate.form()) {
				console.log($('#form-org-add').serialize());
				$.operate.save(prefix + "/add", $('#form-org-add').serialize());
	        }
	    }
	
		/*组织管理-新增-选择父组织树*/
		function selectOrgTree() {
			var treeId = $("#treeId").val();
			if ($.common.isEmpty(treeId)) {
       			$.modal.alertWarning("请先添加用户所属的组织！");
       			return;
       		}
			var options = {
				title: '组织选择',
				width: "380",
				url: prefix + "/selectOrgTree/" + treeId+ "/0",
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
		function doSubmit(index, layero){
			var body = $.modal.getChildFrame(index);
   			$("#treeId").val(body.find('#treeId').val());
   			$("#treeName").val(body.find('#treeName').val());
   			$.modal.close(index);
		}
	</script>
</body>
</html>
